package com.globagames.strategicgames.command
{
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.globagames.isometric.core.business.FileAccessDelegate;
	import com.globagames.isometric.core.model.Block;
	import com.globagames.isometric.core.model.IMapModule;
	import com.globagames.isometric.core.model.IsoTile;
	import com.globagames.strategicgames.event.LoadBattleMapEvent;
	import com.globagames.strategicgames.model.StrategicGameModelLocator;
	
	import flash.geom.Rectangle;
	import flash.system.ApplicationDomain;
	
	import mx.events.ModuleEvent;
	import mx.modules.ModuleLoader;
	import mx.rpc.IResponder;

	public class  LoadBattleMapCommand implements Command, IResponder
	{
		private var delegate : FileAccessDelegate;
		private var e:LoadBattleMapEvent;
		private var currentTask:String;
		
		private var model:StrategicGameModelLocator = StrategicGameModelLocator.getInstance();
		
		public function  LoadBattleMapCommand()
		{
			
		}

		public function execute(event:CairngormEvent):void
		{		
			e = event as LoadBattleMapEvent;
			trace("LoadBattleMapCommand execute");
			e.mapModule = model.mapModules[e.moduleName] as IMapModule;
			if( e.mapModule ) {
				//e.mapModule = e.moduleLoader.child as IMapModule;
				e.isDone = true;	
				 
			} else {
				//moduleLoader = new ModuleLoader();
				e.moduleLoader.addEventListener(ModuleEvent.READY, moduleReadyHandler);
				e.moduleLoader.applicationDomain = ApplicationDomain.currentDomain;
				e.moduleLoader.url = e.moduleDirectoryPath + e.moduleName + "Module.swf";
				//moduleLoader.loadModule();	
			}
		}
		private function moduleReadyHandler(event:ModuleEvent):void {
			trace("LoadBattleMapCommand moduleReadyHandler");
			e.mapModule = e.moduleLoader.child as IMapModule;
			model.mapModules[e.moduleName] = e.mapModule;				
			e.mapModule.initialize2();
			
			delegate = new FileAccessDelegate( this );	  
			currentTask = "loadDesign";
			delegate.loadArrayFile(e.moduleDirectoryPath+"design.dsg");
		
																								
		}
		public function result( event : Object ) : void
		{	
			var s:Array ;
			var c : IsoTile;
			var j: int;
			var y : int = 0;
			if ( currentTask == "loadDesign" ) {
				
				var arr:Array = event as Array;
				for(var i:int=0;i< arr.length;i++)
				{
					var line:String = arr[i] as String;
					if ( line.indexOf("#") == 0 ) {
						state = line ;
					} else {
						if( state == "#size" ) {
							s = line.split(",");	
							e.mapModule.sizeX = Number(s[0]);
							e.mapModule.sizeY = Number(s[1]);							
							e.mapModule.tilesTable = new Array(e.mapModule.sizeY);
							for(var iy:int=0 ; iy < e.mapModule.sizeY; iy++){	
								e.mapModule.tilesTable[iy] = new Array(e.mapModule.sizeX);				
								for (var ix:int=0;ix<e.mapModule.sizeX;ix++){	
									var t:IsoTile = new IsoTile();
									t.x2d = ix;
									t.y2d = iy;
									t.x3d = ix*32 - iy*32;
									t.y3d = ( ix*32 + iy*32 )/2;
									t._depth = t.y3d + 16;		
									//trace(	"t._depth="+t._depth);							
									e.mapModule.tilesTable[iy][ix] = t;
								}
							}	
			
						}	else if( state == "#tiles" ) {
							s = line.split("|");							
							for(var x:int = 0 ; x < s.length ; x++ ) {
								var isotile:IsoTile = e.mapModule.tilesTable[y][x] as IsoTile;
								var tt :Array = ( s[x] as String ).split(",");
								for(var it:int = 0 ; it < tt.length ; it++ ) {
									isotile.design.push(Number(tt[it]));
								}
							}
							y++;							
						}
					}
				}				
				currentTask = "loadArchitecture";
				delegate.loadArrayFile(e.moduleDirectoryPath+"architecture.arc");
			} else 
			if ( currentTask == "loadArchitecture" ) {
				//trace("LoadBattleMapCommand loadArchitecture");	
				var nbArtworks:int = 0;
				var state:String ="";
				var s:Array ;
				var arr:Array = event as Array;
				e.mapModule.bitmapsRects = new Array();
				for(var i:int=0;i< arr.length;i++)
				{
					var line:String = arr[i] as String;
					if ( line.indexOf("#") == 0 ) {
						state = line ;
					} else {
						if( state == "#artworks" ) {
							nbArtworks = Number(line);
						} else if ( state == "#blocks" ) {
							s = (line as String).split(",");
							var b:Block = new Block();
							b.bitmapNumber=Number(s[1]);
							b.rect =  new Rectangle(Number(s[2]),Number(s[3]),Number(s[4]),Number(s[5]));
							b.type = Number(s[6]);
							e.mapModule.bitmapsRects .push(b);
						}else if ( state == "#visibility blocks" ) {
							s = (line as String).split(",");
							var b:Block = e.mapModule.bitmapsRects[Number(s[0])] as Block;
							var b2:Block = e.mapModule.bitmapsRects[Number(s[1])] as Block;
							b.visibilityBlock = b2;
						}
					}
				}
				e.mapModule.mapName = e.moduleName;					
				e.isDone = true;					
			}
			
		}
		
		public function fault( event : Object ) : void
		{			
			trace(event.fault.rootCause.message, event.fault.faultCode);
		}
		
	}
}